import React, { Component } from 'react';
//引入PropTypes插件
import PropTypes from 'prop-types';

class Comment extends Component {
  //验证评论内容为必填项
  static propTypes = {
    comment: PropTypes.object.isRequired
  };

  constructor() {
    super();
    this.state = {
      timeString: ''
    };
  }

  componentWillMount() {
    this._updateTimeString();
  }

  _updateTimeString() {
    //获取评论的内容
    const comment = this.props.comment;
    const duration = (+Date.now() - comment.createdTime) / 1000;
    this.setState({
      timeString: duration > 60 ? `${Math.round(duration / 60)} 分钟前` : `${Math.round(Math.max(duration, 1))} 秒前`
    });
  }

  render() {
    return (
      <div className="comment">
        <div className="comment-user">
          <span>{this.props.comment.userName}</span>：
        </div>

        <p>{this.props.comment.content}</p>
        <span className="comment-createdtime">{this.state.timeString}</span>
        <span className="comment-delete">删除</span>
      </div>
    );
  }
}

export default Comment;
